package com.fau.wm.action.index;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalOrdersAlreadyDelivery;
import com.fau.wm.util.FileNameEncoderUtil;
import com.fau.wm.util.SystemConfig;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 订单签收Action
 * @author kangyj
 * @datetime 2014年11月29日,下午3:48:42
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class OrdersSignAction extends PublicAction implements
		ModelDriven<MedicalOrdersAlreadyDelivery> {

	private static final long serialVersionUID = 8212378153148623764L;
	
	private MedicalOrdersAlreadyDelivery ordersAlreadyDelivery = new MedicalOrdersAlreadyDelivery();
	
	/**
	 * @desc 订单签收页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月29日,下午3:49:57
	 */
	@Action(value = "orders_sign_init", results = {@Result(name = SUCCESS, location = "orders_sign_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 弹出批量导入签收订单信息
	 * @return SUCCESS
	 * @author dangjh
	 * 2014年12月26日 上午2:54:29
	 */
	@Action(value = "orders_sign_info_init", results = {@Result(name = SUCCESS, location = "orders_sign_info_init.jsp")})
	public String initInfo() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询已发货订单
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月28日,下午10:10:05
	 */
	@Action(value = "orders_sign_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ordersAlreadyDelivery.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getOrdersManagerService().findOrdersAlreadyDelivery(ordersAlreadyDelivery, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量签收
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月29日,下午4:39:54
	 */
	@Action(value = "orders_sign")
	public String sign() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(StringUtils.isNotBlank(ordersAlreadyDelivery.getId())) {
			this.baseAllService.getOrdersManagerService().ordersSign(admin, ordersAlreadyDelivery.getId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量网签
	 * @return null
	 * @author liy
	 * @datetime 2014年12月13日,下午3:10:14
	 */
	@Action(value = "orders_net_sign")
	public String netSign() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(StringUtils.isNotBlank(ordersAlreadyDelivery.getId())) {
			this.baseAllService.getOrdersManagerService().ordersNetSign(admin, ordersAlreadyDelivery.getId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量导入签收初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月29日,下午6:18:23
	 */
	@Action(value = "orders_sign_import_init", results = {@Result(name = SUCCESS, location = "orders_sign_import_init.jsp")})
	public String importSignInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 导入签收
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月29日,上午12:10:55
	 */
	@Action(value = "orders_sign_import")
	public String importSign() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request);
		try {
			Map<String, Object> temp = this.baseAllService.getOrdersManagerService().importSign(file);
			map.put("errorCount", temp.get("info"));
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量修改物流公司
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月14日,下午9:29:20
	 */
	@Action(value = "orders_edit_logistics")
	public String editLogistics() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getOrdersManagerService().editLogistics(ordersAlreadyDelivery);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 下载药品签收模版
	 * @return null
	 * @author liy
	 * @datetime 2014年12月26日,下午12:19:18
	 */
	@Action(value = "orders_sign_download")
	public String medicineDownload() {
		this.download(SystemConfig.MEDICINE_SIGN_MASTERPLATE, "批量导入签收.xls");
		return null;
	}
	
	/**
	 * @desc 订单签收-导出当前页
	 * @return null
	 * @author liy
	 * @datetime 2014年12月28日,上午11:03:39
	 */
	@Action(value = "orders_sign_export_current")
	public String currentExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"订单签收导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().exportSignOrdersCurrent(this.ordersAlreadyDelivery.getId());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 订单签收-导出全部
	 * @return null
	 * @author liy
	 * @datetime 2014年12月28日,下午4:03:23
	 */
	@Action(value = "orders_sign_export_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"订单签收导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().exportSignOrdersAll(this.ordersAlreadyDelivery);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalOrdersAlreadyDelivery getModel() {
		return ordersAlreadyDelivery;
	}

}
